package gui;

import entity.ExamInfo;
import entity.QuestionInfo;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;

import util.MyFactory;

//TODO day01 设置答题界面
public class ExamFrame extends BaseFrame {
	private static final long serialVersionUID = 2160757960742126375L;
	/**考试信息对象*/
	private ExamInfo examInfo;
	/**包含用户答案的题目对象*/
	private QuestionInfo questionInfo;
	/**显示题目*/
	private JTextArea questionArea;
	/**多选框的集合*/
	private List<JCheckBox> options=new ArrayList<JCheckBox>();
	/**用户答案的集合*/
	private List<Integer> userAnswer;
	/**上一题按钮*/
	private JButton prev;
	/**下一题按钮*/
	private JButton next;
	/**计时器，用于处理倒计时问题*/
	private Timer timer=new Timer();
	/**显示倒计时*/
	private JLabel timeJL;
	/**题号选择输入框*/
	private JTextField choose;
	public ExamFrame() {
		init();
	}
	public ExamFrame(ClientContext context){
		this();
		this.context=context;
	}
	@Override
	protected void init() {
		this.setTitle("学生在线考试系统");
		this.setSize(600, 400);
		//居中显示窗体
		this.setLocation((WEIGHT - 600) / 2, (HEIGHT - 400) / 2);
		
		createScrollPane();
		createJButtons();
		createJCheckBox();
		createTime();
		createChoose();
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}
	/**创建选择题号的面板*/
	private void createChoose() {
		JButton jb=new JButton("跳转");
		jb.setBounds(450, 290, 60, 25);
		this.add(jb);
		choose=new JTextField();
		choose.setBounds(520, 290, 38, 25);
		choose.setBorder(new SoftBevelBorder(1));
		jb.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				context.choose(choose.getText());
			}
		});
		this.add(choose);
	}
	/**创建显示时间*/
	private void createTime() {
		timeJL=new JLabel();
		timeJL.setBounds(20, 290,200,20);
		timeJL.setFont(new Font("宋体",Font.ITALIC+Font.BOLD, 14));
		this.add(timeJL);
	}
	/**创建多选框*/
	private void createJCheckBox() {
		JCheckBox a = getCheckBox("A");
		JCheckBox b = getCheckBox("B");
		JCheckBox c = getCheckBox("C");
		JCheckBox d = getCheckBox("D");
		a.setBounds(200, 290, 40, 20);
		b.setBounds(250, 290, 40, 20);
		c.setBounds(300, 290, 40, 20);
		d.setBounds(350, 290, 40, 20);
	}
	/**创建按钮*/
	private void createJButtons() {
		prev = new JButton("上一题");
		next = new JButton("下一题");
		JButton send = new JButton("交卷");
		
		next.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				context.next();
			}
		});
		prev.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				context.prev();
			}
		});
		send.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				context.send();
			}
		});
		prev.setBounds(180-20, 330, 80, 30);
		next.setBounds(280-20, 330, 80, 30);
		send.setBounds(380-20, 330, 80, 30);
		this.add(prev);
		this.add(next);
		this.add(send);
	}
	/**创建滚动面板*/
	private void createScrollPane() {
		JScrollPane pane = new JScrollPane();
		pane.setBounds(8, 90, 580, 200);
		pane.setBorder(new TitledBorder("题目"));
		questionArea = new JTextArea();
		//questionArea.setText("问题\nA.\nB.\n\n\n\n\\n\n\n\\n\n\n\\n");
		questionArea.setWrapStyleWord(true);
		questionArea.setLineWrap(true);
		questionArea.setEnabled(false);
		//questionArea.setEditable(false);
		questionArea.setDisabledTextColor(Color.black);
		pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		pane.getViewport().add(questionArea);
		pane.setOpaque(false);
		this.add(pane);
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		ImageIcon back = MyFactory.getImageIcon("exam-back.png");
		JLabel jl = new JLabel(back);
		jl.setBounds(0, 0, 600, 400);
		this.add(jl);
		g.setFont(new Font("",Font.BOLD,12));
		g.drawString(examInfo.toString(), 20, 100);
		g.drawString("题号：" + examInfo.getQuestionCount() + "题中的第"
					+ (questionInfo.getQuestionIndex() + 1) + "题", 20, 370+7);
		//g.drawString("剩余时间："+lastTime+"秒", );
		
		ImageIcon icon = MyFactory.getImageIcon("exam_title.png");
		g.drawImage(icon.getImage(), (600 - 364) / 2, 30, 364, 58, this);
	}
	/**创建多选框的工厂方法*/
	public JCheckBox getCheckBox(String name) {
		JCheckBox jcb = new JCheckBox(name);
		jcb.setOpaque(false);
		options.add(jcb);
		this.add(jcb);
		
		return jcb;
	}
	/**更新界面*/
	public void updateView() {
		examInfo=context.getExamInfo();
		updateQuestions();
		startTimer();
	}
	/**更新问题*/
	public void updateQuestions(){
		questionInfo=context.getQuestionInfo();
		questionArea.setText(questionInfo.getQuestion().toString());
		/*System.out.println("题号：" + examInfo.getQuestionCount() + "题中的第"
					+ (questionInfo.getQuestionIndex() + 1) + "题");*/
		updateJCheckBox();
		updateJButton();
		repaint();
	}
	/**更新按钮状态*/
	private void updateJButton() {
		int index = context.getQuestionInfo().getQuestionIndex();
	    int max = context.getExamInfo().getQuestionCount();
	    next.setEnabled(index<(max-1));
	    prev.setEnabled(index>0);
	}
	/**更新多选框状态*/
	public void updateJCheckBox(){
		//增加新的功能，根据题目的选项判断应该出现几个多选框。
		int optionNums=questionInfo.getQuestion().getOptionNums();
		System.out.println("optionNums"+optionNums);
		for (int i = optionNums; i < options.size(); i++) {
			options.get(i).setEnabled(false);
		}
		for (int i = 0; i < optionNums; i++) {
			options.get(i).setEnabled(true);
		}
		//读取题目信息，判断本题目多选框哪几个已经选择了
		for(Integer i:questionInfo.getUserAnswers()){
			options.get(i).setSelected(true);
		}
	}
	/**得到用户的答案*/
	public List<Integer> getUserAnswer() {
		int index=0;
		userAnswer=new ArrayList<Integer>();
		for(JCheckBox jcb:options){
			if (jcb.isSelected()) {
				jcb.setSelected(false);
				userAnswer.add(index);
			}
			index++;
		}
		System.out.println(userAnswer);
		return userAnswer;
	}
	/**开始计时*/
	public void startTimer(){
		final int timeLimit=examInfo.getTimeLimit();
		final long startTime=System.currentTimeMillis();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				long endTime=System.currentTimeMillis();
				long time=timeLimit*60*1000-endTime+startTime;
				showTime(time);
				if (time<=0) {
					timer.cancel();
					context.gameOver();
				}
			}
			
		}, 0, 1000);
	}
	/**显示时间*/
	private void showTime(long time) {
		int hour=(int)(time/1000/60/60);
		int minute=(int)(time/1000/60%60);
		int second=(int)(time/1000%60);
		String lastTime=(hour<10?("0"+hour):hour)+":"+(minute<10?("0"+minute):minute)+":"+(second<10?("0"+second):second);
		
		timeJL.setText("倒计时："+lastTime);
		if (minute<=examInfo.getTimeLimit()/4) {
			timeJL.setForeground(Color.RED);
		}
	}
}
